//
//  HGHomeVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/28.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGHomeVC.h"
#import "HomeHeaderView.h"
#import "UIView+Extension.h"
#import "HGHomeModel.h"
#import "HGGoodsModel.h"
#import "HGGoodsDetailVC.h"
#import "BaseWKWebviewVC.h"
#import "HGScanCodeVC.h"

@interface HGHomeVC ()

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) HomeHeaderView *headrView;

@property (strong, nonatomic) HomeSecondView *secondView;

@property (strong, nonatomic) NSMutableArray <HGGoodsModel *> *goodsModels;

@property (strong, nonatomic) NSMutableArray <HomeGoodsView *> *goodViews;

@end

@implementation HGHomeVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self initDatas];
    
}

- (void)initSubViews {
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    self.navigationItem.titleView = imageView;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"saoyisao"] style:(UIBarButtonItemStyleDone) target:self action:@selector(sacleCode)];
    _headrView = [[HomeHeaderView alloc] init];
    [self.scrollView addSubview:_headrView];
    [_headrView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 180));
    }];
    
    DistrictHead *headOne = [[DistrictHead alloc] initWithTitle:@"酒圈子"];
    [_scrollView addSubview:headOne];
    [headOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headrView.mas_bottom);
        make.left.right.equalTo(_headrView);
        make.height.offset(50);
    }];
    
    _secondView = [[HomeSecondView alloc] init];
    _secondView.callBack = ^(NSString *url) {
        BaseWKWebviewVC *vc = [[BaseWKWebviewVC alloc] initWithUrl:url];
        vc.diy_title = @"酒圈子";
        [self.navigationController pushViewController:vc animated:YES];
    };
    [self.scrollView addSubview:_secondView];
    [_secondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headOne.mas_bottom);
        make.left.right.equalTo(self.headrView);
        make.height.offset(150);
    }];
    
    DistrictHead *headTwo = [[DistrictHead alloc] initWithTitle:@"精选美酒"];
    [_scrollView addSubview:headTwo];
    [headTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_secondView.mas_bottom);
        make.left.right.equalTo(_secondView);
        make.height.offset(50);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    [self.scrollView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headTwo.mas_bottom).offset(-15);
        make.left.equalTo(_scrollView);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, kScreenWidth/2 * 2));
    }];
    
    _goodViews = [@[] mutableCopy];
    CGSize size = CGSizeMake(kScreenWidth/2, kScreenWidth/2);
    for (int i = 0; i < 4; i++) {
        int row = i / 2;
        int col = i % 2;
        HomeGoodsView *goodsView = [[HomeGoodsView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showDetail:)];
        [goodsView addGestureRecognizer:tap];
        [bgView addSubview:goodsView];
        [goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(size.height * col);
            make.left.offset(size.height * row);
            make.size.mas_equalTo(size);
        }];
        [_goodViews addObject:goodsView];
    }
    
    [self.view layoutIfNeeded];
    
    [_scrollView setContentSize:CGSizeMake(kScreenWidth, _headrView.height + _secondView.height + bgView.height + headOne.height * 2)];
}

#pragma mark - 商品的点击
- (void)showDetail:(UITapGestureRecognizer *)tap {
    HomeGoodsView *view = (HomeGoodsView *)tap.view;
    HGGoodsDetailVC *vc = [[HGGoodsDetailVC alloc] init];
    vc.goodId = view.model.goodsId;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 轮播图 商品接口
- (void)initDatas {
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/ad/search") parameters:@{@"position" : @1} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            NSArray *imageArr = [HGHomeModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
            _headrView.imageArray = imageArr;
        }
    }];
    
    
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/product/list") parameters:nil completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            NSArray *goods = [HGGoodsModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
            
            NSArray *imagNames = @[@"hot",@"new",@"90",@"80"];
            [_goodViews enumerateObjectsUsingBlock:^(HomeGoodsView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                obj.model = goods[idx];
                obj.leftImage.image = [UIImage imageNamed:imagNames[idx]];
            }];
        }
    }];
    
    [[LMJRequestManager sharedManager] GET:KPHP_Url(@"index.php?act=/Api/Article/banner") parameters:nil completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            _secondView.homeModels = [HGHomeModel mj_objectArrayWithKeyValuesArray:response.responseModel.data[@"rows"]];
        }
    }];
}

#pragma mark - 扫描酒信息
- (void)sacleCode {
    [self.navigationController pushViewController:[HGScanCodeVC new] animated:YES];
}


- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = kColorBackground;
        [self.view addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.bottom.offset(-kBottomBarHeight);
        }];
    }
    return _scrollView;
}

- (UIColor *)navigationColor {
    return kColorMainTheme;
}

- (BOOL)navigationIsLight {
    return YES;
}

@end
